<%@page import="com.healtyprj.Constants"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.amazonaws.util.json.JSONArray"%>
<%@page import="java.util.List"%>
<%@page import="com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression"%>
<%@page import="com.healtyprj.Goal"%>
<%@page import="com.healtyprj.User"%>
<%@page import="com.healtyprj.Weight"%>
<%@page import="com.amazonaws.util.json.JSONObject"%>
<%@page import="org.springframework.util.StringUtils"%>
<%@page import="com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper"%>
<%@page import="com.amazonaws.services.dynamodb.model.PutItemResult"%>
<%@page import="com.amazonaws.services.dynamodb.model.PutItemRequest"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="com.amazonaws.services.dynamodb.model.AttributeValue"%>
<%@page import="com.amazonaws.AmazonServiceException"%>
<%@page import="com.amazonaws.services.dynamodb.model.TableStatus"%>
<%@page import="com.amazonaws.services.dynamodb.model.DescribeTableRequest"%>
<%@page import="com.amazonaws.services.dynamodb.model.TableDescription"%>
<%@page import="com.amazonaws.services.dynamodb.model.ProvisionedThroughput"%>
<%@page import="com.amazonaws.services.dynamodb.model.KeySchemaElement"%>
<%@page import="com.amazonaws.services.dynamodb.model.KeySchema"%>
<%@page import="com.amazonaws.services.dynamodb.model.CreateTableRequest"%>
<%@page import="com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider"%>
<%@page import="com.amazonaws.services.dynamodb.AmazonDynamoDBClient"%>
<%@page import="com.amazonaws.services.dynamodb.AmazonDynamoDB"%>
<%@page import="com.amazonaws.services.dynamodb.model.Condition"%>
<%@page import="com.amazonaws.services.dynamodb.model.ComparisonOperator"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/x-json; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String name = new String(request.getParameter("name").getBytes("8859_1"), "UTF-8");
AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider());
dynamoDB.setEndpoint(Constants.REGION);

DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDB);
//ハッシュキーを設定
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Condition rangeKeyCondition = new Condition()
	.withComparisonOperator(ComparisonOperator.GT.toString())
	.withAttributeValueList(new AttributeValue().withS(format.format(new Date())));
DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression(
		new AttributeValue().withS(name));
queryExpression.setRangeKeyCondition(rangeKeyCondition);
List<Goal> goals = dynamoDBMapper.query(Goal.class, queryExpression);
List<Goal> descGoals = new ArrayList<Goal>();
for (Goal goal : goals) {
	descGoals.add(0, goal);
}
queryExpression = new DynamoDBQueryExpression(
		new AttributeValue().withS(name));
List<Weight> weights = dynamoDBMapper.query(Weight.class, queryExpression);
List<Weight> descWeights = new ArrayList<Weight>();
for (Weight weight : weights) {
	descWeights.add(0, weight);
}
JSONObject json = new JSONObject();
if (!descGoals.isEmpty()
		&& !descWeights.isEmpty()) {
	json.put("finishDate", descGoals.get(0).getFinishDate());
	json.put("goalWeight", descGoals.get(0).getWeight());
	json.put("nowWeight", descWeights.get(0).getWeight());
}
out.print(json.toString());
%>